<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filter" id="form">
        <el-form-item>
          <el-input v-model="filter.username" placeholder="操作用户"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="filter.ip" placeholder="IP地址"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="filter.methodName" placeholder="操作方法"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="filter.module" placeholder="模块"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-refresh" @click="resetForm">重置</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-circle-plus-outline" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="logs" highlight-current-row v-loading="listLoading" @selection-change="selsChange" border
              style="width: 100%;">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column type="index" width="60" label="序号"></el-table-column>
      <el-table-column prop="username" min-width="40px" label="操作用户"></el-table-column>
      <el-table-column prop="description" label="操作描述" min-width="50px"></el-table-column>
      <el-table-column prop="methodName" label="操作方法" min-width="90px"></el-table-column>
      <el-table-column prop="module" label="操作模块" min-width="50px"></el-table-column>
      <el-table-column prop="ip" label="IP地址" min-width="60px"></el-table-column>
      <el-table-column prop="createTime" label="创建时间" min-width="70px"></el-table-column>
      <el-table-column prop="" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑
          </el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete" @click="handleDel(scope.$index, scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>


    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" icon="el-icon-delete-solid" @click="batchRemove" :disabled="this.sels.length===0">批量删除
      </el-button>
      <!--      style="float:right;-->

      <!--  分页插件  -->
      <el-pagination
          background
          layout="total,sizes, prev, pager, next, jumper"
          :total="total" :current-page="currentPage" :page-size="pageSize"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
          :page-sizes="pageSizes"
          style="float:right">
      </el-pagination>
    </el-col>

    <!--编辑/新增界面-->
    <el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false" @close="cancel">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="操作用户" prop="username">
          <el-input v-model="editForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="模块名称" prop="module">
          <el-input v-model="editForm.module" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="方法名" prop="methodName">
          <el-input v-model="editForm.methodName" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="IP地址" prop="ip">
          <el-input v-model="editForm.ip" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="操作介绍" prop="description">
          <el-input v-model="editForm.description" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="cancel">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>

export default {
  data() {
    return {
      filter: {
        username: '',
        module: '',
        ip: '',
        methodName: ''
      },
      title: '新增',
      logs: [],
      total: 0,
      currentPage: 1,
      pageSize: 5,
      pageSizes: [5, 10, 20, 50, 100],
      listLoading: true,
      sels: [],//列表选中列
      nshow: false,
      show: true,
      deptTrees: [],

      editFormVisible: false,// 编辑/添加界面是否显示
      editLoading: false,
      editFormRules: {
        username: [
          {required: true, message: '请输入操作用户', trigger: 'blur'}
        ],
        module: [
          {required: true, message: '请输入记录模块名称', trigger: 'blur'}
        ],
        methodName: [
          {required: true, message: '请输入方法名称', trigger: 'blur'}
        ],
        description: [
          {required: true, message: '请输入详细介绍', trigger: 'blur'}
        ],
        ip: [
          {required: true, message: '请输入IP地址', trigger: 'blur'}
        ],
      },
      //编辑界面数据
      editForm: {
        id: null,
        username: '',
        module: '',
        ip: '',
        methodName: '',
        description: ''
      },
    }
  },
  methods: {

    //重置表单
    resetForm() {
      this.filter.username = ''
      this.filter.ip = ''
      this.filter.methodName = ''
      this.filter.module = ''
      this.getlogs()
    },
    // 删除操作
    deleteDept(index, row) {
      this.$confirm("确定要删除该部门嘛？", '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.delete('/dept/del/' + row.id).then((res) => {
          if (res.success) {
            this.$message({
              message: res.data.msg,
              type: 'success'
            })
          } else {
            this.$message.error(res.data.msg)
          }
        }).finally((r) => {
          this.getlogs()
        })
      })
    },

    // 取消操作
    cancel() {
      this.editForm.username = ''
      this.editForm.id = null
      this.editForm.methodName = ''
      this.editForm.module = ''
      this.editForm.description = ''
      this.editForm.ip = ''
      this.editFormVisible = false
    }
    ,
    // 查询
    search() {
      this.currentPage = 1
      this.getlogs()
    }
    ,
    // 当前页改变
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getlogs();
    }
    ,
    // 当前页数量改变
    handleSizeChange(val) {
      this.pageSize = val;
      this.getlogs()
    }
    ,
    //获取类型列表
    getlogs() {
      // 分页请求参数
      let param = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        id: this.editForm.id,
        module: this.filter.module,
        username: this.filter.username,
        ip: this.filter.ip,
        methodName: this.filter.methodName,
      };
      this.listLoading = false;
      this.$http.post('/log/list', param).then(res => {
        this.logs = res.data.data
        this.total = res.data.totals
        this.listLoading = false
        console.log(res)
      });
    }
    ,
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        let para = {id: row.id};
        this.$http.delete('/log/del/' + row.id).then((res) => {
          this.listLoading = false;
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.currentPage = 1
          this.getlogs();
        });
      }).catch(() => {
      });
    }
    ,
    //显示编辑界面
    handleEdit: function (index, row) {
      this.title = '修改'
      this.editFormVisible = true;
      this.editForm = Object.assign({}, row);
    }
    ,
    //显示新增界面
    handleAdd: function () {
      this.editFormVisible = true;
    }
    ,
    //编辑或新增
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            //复制对象
            let para = Object.assign({}, this.editForm);
            this.$http.post('/log/saveOrUpdate', para).then((res) => {
              if (res.data.success) {
                this.editLoading = false;
                this.$message({
                  message: res.data.msg,
                  type: 'success'
                });
              } else {
                this.$message({
                  message: res.data.msg,
                  type: 'error'
                });
              }
              this.$refs['editForm'].resetFields();
              this.cancel();
            }).finally(res => {
              this.getlogs();
            });
          });
        }
      });
    }
    ,


    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      const ids = this.sels.map(item => item.id); // 返回id数组
      console.log(ids);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        let para = {ids: ids};
        this.$http.patch('/log/delByBatch', para).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '批量删除成功',
            type: 'success'
          });
          this.currentPage = 1
          this.getlogs();
        });
      }).catch(() => {
        this.$message({
          message: '取消删除',
          type: 'info'
        });
      });
    }
  }
  ,
  mounted() {
    this.getlogs();
  }
}

</script>

<style scoped>

.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
</style>